/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.smartwork.common.mobile.wechat.util;


import com.smartwork.common.mobile.exception.BizException;
import com.smartwork.common.mobile.wechat.bean.WechatConf;
import com.smartwork.common.mobile.wechat.dao.WechatConfDao;
import com.smartwork.common.utils.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 微信配置工具类
 * @author Yanm
 * @version 2015-11-11
 */
public class WeixinConfUtils {

    private static Logger log = LoggerFactory.getLogger(WeixinConfUtils.class);
	
	private static WechatConfDao wechatConfDao = SpringContextHolder.getBean("wechatConfDao");

    private static List<WechatConf> allConf = null;

    private static Map<String, WechatConf> allMap = new HashMap<String, WechatConf>();

	public static String[] getAppIDAndAppSecretByGroupId(String groupId) {
        String[] strs = new String[2];
        try {
            init();
            strs[0] = getValue(groupId, "appID");
            strs[1] = getValue(groupId, "appsecret");
        } catch (Exception e) {
            log.error("{}", e);
        }
        return strs;
    }


    public static String getValue(String groupId, String key) {
        try {
            init();
            for(int i = 0 ; i < allConf.size();i++){
                WechatConf bean = allConf.get(i);
                if (groupId.equals(bean.getGroupId()) && key.equals(bean.getEnglishName())) {
                    return bean.getValue();
                }
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
        return "";
    }


    public static void setValue(String groupId, String key, String value) {
        try {
            init();
            for(int i = 0 ; i < allConf.size();i++){
                WechatConf bean = allConf.get(i);
                if (groupId.equals(bean.getGroupId()) && key.equals(bean.getEnglishName())) {
                    bean.setValue(value);
                    wechatConfDao.updateValue(bean);
                }
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
    }

    public static Map<String, String> getValues(String groupId, String type) {
        Map<String, String> map = new HashMap<String, String>();
        try{
            init();

            for(int i = 0 ; i < allConf.size();i++){
                WechatConf bean = allConf.get(i);
                if (groupId.equals(bean.getGroupId()) && type.equals(bean.getType())) {
                    map.put(bean.getEnglishName(), bean.getValue());
                }
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
        return map;
    }

    public static Set<String> getGroupIds() {
        Set<String> groupIds = new HashSet<String>();
        try{
            init();
            for(int i = 0 ; i < allConf.size();i++){
                groupIds.add(allConf.get(i).getGroupId());
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
        return groupIds;
    }

    private static void init() throws Exception {
        if (null == allConf || allConf.size() < 1) {
            allConf = wechatConfDao.findAllList(new WechatConf());

            if (allConf.size() < 1) {
                throw new BizException(4200, "数据不存在！");
            }

        }
    }
}
